﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VCC.Lib.Paging;
using System.Linq.Dynamic;

namespace VCC.Model
{
    public class ProductManager
    {
        /*
        // Modifield by Vietlv
        // Description: added parameter Guid addby
        public static IPagedList<Product> GetList(int? typeid,int? catid,int? cityid, int? distrid, int? areaid, int? priceid,int? projectid, int? timeid, string keyword, int hotYN, int pubYN, string order, string orderDir, int page, int pageSize, Guid? addby )
        {
            var dataContext = new ChoVietDataContext();
            var fromDate = DateTime.Now;
            var endDate = DateTime.Now;
            if (timeid > 0) {
                switch (timeid)
                { 
                    case 1:
                        fromDate = DateTime.Now.AddDays(-6); // For Week
                        break;
                    case 2:
                        fromDate = DateTime.Now.AddMonths(-1);// For Month
                        break;
                    case 3:
                        fromDate = DateTime.Now.AddMonths(-3);// For Qui
                        break;
                    case 4:
                        fromDate = DateTime.Now.AddYears(-1);// For Year
                        break;
                }
            }
            var l = dataContext.Products
                .Where(a => typeid > 0 ? a.TypeID==typeid : true)
                .Where(a => catid > 0 ? a.CatID==catid : true)
                .Where(a => cityid > 0 ? a.CityID == cityid : true)
                .Where(a => distrid > 0 ? a.DistrictID == distrid : true)
                .Where(a => areaid > 0 ? a.ConvertArea == areaid : true)
                .Where(a => priceid > 0 ? a.ConvertPrice == priceid : true)
                .Where(a => projectid > 0 ? a.ProjectID == projectid : true)
                .Where(a => timeid > 0 ? a.AddDate>=fromDate && a.AddDate<=endDate : true)
                .Where(a => hotYN > 0 ? a.HotYN == (hotYN == 1 ? true : false) : true)
                .Where(a => pubYN > 0 ? a.PubYN == (pubYN == 1 ? true : false) : true)
                .Where(a => addby != null ? a.AddBy == addby : true)  // Added by Vietlv addDate: 12/09/2011
                .Where(a => keyword == string.Empty ? true : (a.Title.Contains(keyword.ToLower()) || (a.Summary.ToLower().Contains(keyword.ToLower()))))
                .OrderBy(string.Format("{0} {1}", order, orderDir)).AsQueryable();
            var returnVal = l.ToPagedList(page, pageSize);
            return returnVal;
        }
        /// <summary>
        /// Created by Vietlv
        /// Created date: 12/09/2011
        /// Description: getlist product of current user
        /// </summary>
        /// <returns></returns>
        public static IPagedList<Product> GetListProductOfUser(int? typeid, int? catid, int? cityid, int? distrid, int? areaid, int? priceid, int? projectid, int? timeid, string keyword, int hotYN, int pubYN, string order, string orderDir, int page, int pageSize, Guid addby)
        {
            var dataContext = new ChoVietDataContext();
            var fromDate = DateTime.Now;
            var endDate = DateTime.Now;
            if (timeid > 0)
            {
                switch (timeid)
                {
                    case 1:
                        fromDate = DateTime.Now.AddDays(-6); // For Week
                        break;
                    case 2:
                        fromDate = DateTime.Now.AddMonths(-1);// For Month
                        break;
                    case 3:
                        fromDate = DateTime.Now.AddMonths(-3);// For Qui
                        break;
                    case 4:
                        fromDate = DateTime.Now.AddYears(-1);// For Year
                        break;
                }
            }
            var l = dataContext.Products
                //  .Where(a => typeid > 0 ? a.TypeID == typeid : true)
                // .Where(a => catid > 0 ? a.CatID == catid : true)
                //  .Where(a => cityid > 0 ? a.CityID == cityid : true)
                //  .Where(a => distrid > 0 ? a.DistrictID == distrid : true)
                //  .Where(a => areaid > 0 ? a.ConvertArea == areaid : true)
                //  .Where(a => priceid > 0 ? a.ConvertPrice == priceid : true)
                // .Where(a => projectid > 0 ? a.ProjectID == projectid : true)
                 //.Where(a => timeid > 0 ? a.AddDate >= fromDate && a.AddDate <= endDate : true)
                 .Where(a => hotYN > 0 ? a.HotYN == (hotYN == 1 ? true : false) : true)
                 .Where(a => pubYN > 0 ? a.PubYN == (pubYN == 1 ? true : false) : true)
                .Where(a => addby != null ? a.AddBy == addby : true) // Added by Vietlv addDate: 12/09/2011
               // .Where(a => keyword == string.Empty ? true : (a.Title.Contains(keyword.ToLower()) || (a.Summary.ToLower().Contains(keyword.ToLower()))))
                .OrderBy(string.Format("{0} {1}", order, orderDir)).AsQueryable();
            var returnVal = l.ToPagedList(page, pageSize);
            return returnVal;
        }
        public static List<int> GetChild(int catid)
        {
            try
            {
                var db = new ChoVietDataContext();
                var returnVal = new List<int>();
                var curentCat = db.ProductCategories.Single(o => o.ID == catid);
                returnVal.Add(curentCat.ID);

                foreach (var item in curentCat.ProductCategories)
                {
                    returnVal.AddRange(GetChild(item.ID));
                }

                return returnVal;
            }
            catch (Exception exp)
            {
                return null;
            }
        }
        public static List<ProductCategory> GetObjs(int catid)
        {
            try
            {
                var db = new ChoVietDataContext();
                var returnVal = new List<ProductCategory>();
                var curentCat = db.ProductCategories.Single(o => o.ID == catid);
                returnVal.Add(curentCat);

                foreach (var item in curentCat.ProductCategories)
                {
                    returnVal.AddRange(GetObjs(item.ID));
                }

                return returnVal;
            }
            catch (Exception exp)
            {
                return null;
            }
        }
        public static List<int> GetChildByType(int typeid)
        {
            try
            {
                var db = new ChoVietDataContext();
                var returnVal = new List<int>();
                var type = db.ProductTypes.Where(e => e.ID == typeid).SingleOrDefault();

                foreach (var item in type.ProductCategories)
                {
                    returnVal.AddRange(GetChild(item.ID));
                }

                return returnVal;
            }
            catch (Exception exp)
            {
                return null;
            }
        }
        public static List<ProductCategory> GetObjChildByType(int typeid)
        {
            try
            {
                var db = new ChoVietDataContext();
                var returnVal = new List<ProductCategory>();
                var type = db.ProductTypes.Where(e => e.ID == typeid).SingleOrDefault();

                foreach (var item in type.ProductCategories)
                {
                    returnVal.AddRange(GetObjs(item.ID));
                }

                return returnVal;
            }
            catch (Exception exp)
            {
                return null;
            }
        }
        public static List<City> GetCity(int typeid, int catid)
        {
            try
            {
                var db = new ChoVietDataContext();
                var l = (from c in db.Cities
                         //where c.Products.Where(e => e.TypeID == typeid && (catid > 0 ? e.CatID == catid : true)).ToList().Count > 0
                         orderby c.Products.Where(e => e.TypeID == typeid && (catid > 0 ? e.CatID == catid : true)).ToList().Count descending
                         select c).ToList();                
                return l;
            }
            catch (Exception exp)
            {
                return null;
            }
        }
        public static List<District> GetDistrict(int typeid, int catid, int cityid)
        {
            try
            {
                var db = new ChoVietDataContext();
                var l = (from c in db.Districts
                         //where c.Products.Where(e => e.TypeID == typeid && e.CityID==cityid && (catid > 0 ? e.CatID == catid : true)).ToList().Count > 0
                         where c.CityID == cityid
                         orderby c.Products.Where(e => e.TypeID == typeid && e.CityID == cityid && (catid > 0 ? e.CatID == catid : true)).ToList().Count descending
                         select c).ToList();  
                return l;
            }
            catch (Exception exp)
            {
                return null;
            }
        }
        public static List<ConvertPrice> GetConvertPrice(int typeid)
        {
            try
            {
                var db = new ChoVietDataContext();
                var l = (from c in db.ConvertPrices
                         where (c.TypeID == typeid)
                         orderby c.ID ascending
                         select c).ToList();
                return l;
            }
            catch (Exception exp)
            {
                return null;
            }
        }
        public static List<ConvertArea> GetConvertArea(int typeid)
        {
            try
            {
                var db = new ChoVietDataContext();
                var l = (from c in db.ConvertAreas
                         where (c.TypeID == typeid)
                         orderby c.ID ascending
                         select c).ToList();
                return l;
            }
            catch (Exception exp)
            {
                return null;
            }
        }
        public static IList<Comment> GetComments(int id, bool? publishYN, int number)
        {
            var dataContext = new ChoVietDataContext();
            var l = (from c in dataContext.Comments
                     from m in dataContext.CommentsInProducts
                     where c.ID == m.CommentID && m.ProductID == id && (publishYN == null ? true : c.PublishYN == publishYN)
                     orderby c.ID descending
                     select c).Take(number).ToList();
            return l;
        }
        public static Product GetProduct(int pId)
        {
            var db = new ChoVietDataContext();
            return db.Products.SingleOrDefault(a => a.ID == pId);
        }
         
         */
    }
}
